package com.tutu.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tutu.reggie.entity.ShoppingCart;
import com.tutu.reggie.mapper.ShoppingCartMapper;
import com.tutu.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ShoppingCartServiceIml extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {


    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    /**
     * 减一的为菜品
     *
     * @param dishId
     */
    @Override
    public void updateDish(Long dishId) {
        //根据dishId查找对应的菜品数量
        Map<String, Long> param = new HashMap<>();
        param.put("dish_id", dishId);
        int number = shoppingCartMapper.findNumberById(param);
        //当数量小于1时 直接删除该条记录
        if (number <= 1) {

            //TODO
            Map<String,Object> idmap=new HashMap<>();
            idmap.put("dish_id",dishId);
           shoppingCartMapper.deleteByMap(idmap);

        } else {
            shoppingCartMapper.updateDish(dishId);
        }
    }

    @Override
    public void updateSetmeal(Long setmealId) {
        //根据setmealId查找对应的套餐的数量
        Map<String, Long> param = new HashMap<>();
        param.put("setmeal_id", setmealId);
        int number = shoppingCartMapper.findNumberById(param);
        if (number <= 1) {
            Map<String,Object> idmap=new HashMap<>();
            idmap.put("sermeal_id",setmealId);
            shoppingCartMapper.deleteByMap(idmap);
            shoppingCartMapper.deleteById(setmealId);
        } else {
            shoppingCartMapper.updateSetmeal(setmealId);
        }
    }
}
